﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShenLan.Calculator
{
    public class Calc
    {
        /// <summary>
        /// 根据输入计算表达式expression计算结果
        /// </summary>
        /// <param name="expression">计算表达式expression</param>
        /// <param name="result">输出结果</param>
        /// <returns>true为计算正确,false为计算出错</returns>
        public static bool GetRuselt(string expression, out double result)
        {
            try
            {
                GrammerAnalyzer ga = new GrammerAnalyzer(expression);
                ga.Analyze();

                Token[] toks = ga.TokenList;
                SyntaxAnalyzer sa = new SyntaxAnalyzer(toks);
                sa.Analyze();

                Calculator calc = new Calculator(sa.SyntaxTree);
                double value = calc.Calc();
                result = value;
                return true;
            }
            catch (Exception e)
            {
                result = 0;
                return false;
            }
        }
    }
}